package com.andy.service.impl;

import com.andy.dao.MyDao;
import com.andy.dao.entity.TAccount;
import com.andy.service.AndyService;
import com.andy.service.LagouService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AndyServiceImpl implements AndyService{

    @Autowired
    private LagouService lagouService;

    @Autowired
    private MyDao myDao;

    @Override
    @Transactional
    public void transfer(String fromCardNo, String toCardNo, Double amount) throws Exception {
        //先把卡查询出来
        TAccount from = myDao.queryAccountByCardNo(fromCardNo);
        TAccount to = myDao.queryAccountByCardNo(toCardNo);

        //赋值
        from.setBalanceAmount(from.getBalanceAmount()-amount);
        to.setBalanceAmount(to.getBalanceAmount()+amount);

        //保存
        myDao.updateCustBalanceAmount(from);

        //故意抛错，测试是否回滚
        if(amount==100){
            throw new RuntimeException();
        }

        myDao.updateCustBalanceAmount(to);
    }

    @Override
    public void teach() {
        lagouService.teach();
    }

}
